package edu.uic.cs.i422.s2011.team4;

import java.util.Calendar;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;

/*
 * This class was found in parts online and combined together to make one
 * cohesive class.
 * 
 * http://developer.android.com/resources/tutorials/views/hello-datepicker.html
 * http://developer.android.com/resources/tutorials/views/hello-timepicker.html
 * 
 */
public class DateTimeDisplay extends Activity implements OnClickListener {

	private TextView mDateDisplay;
	private Button mPickDate;
	private TextView mTimeDisplay;
	private Button mPickTime;
	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;

	static final int DATE_DIALOG_ID = 0;
	static final int TIME_DIALOG_ID = 1;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//create a view and get the layout from res/layout/datetime.xml
		View inflatedView = View.inflate(this, R.layout.datetime, null);
        SquirrelUIGenerator squirrel = new SquirrelUIGenerator(this);
        //set title, help and breadcumb info from res/values/datetime.xml
        squirrel.setTitle(R.string.datetimeTitle);
        squirrel.setHelp(R.string.datetimeHelp);
        squirrel.setBreadCrumb(R.string.datetimeBreadCrumb);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL); //Set Orientation
        layout.addView(squirrel.getHeader());
        layout.addView(inflatedView);
        setContentView(layout);

		// capture our View elements
		mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
		mPickDate = (Button) findViewById(R.id.pickDate);

		// add a click listener to the button
		mPickDate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		if(!SquirrelData.getDateTimeCheckMark())
		{
			// get the current date
			final Calendar c = Calendar.getInstance();
			mYear = c.get(Calendar.YEAR);
			mMonth = c.get(Calendar.MONTH);
			mDay = c.get(Calendar.DAY_OF_MONTH);
			mHour = c.get(Calendar.HOUR_OF_DAY);
			mMinute = c.get(Calendar.MINUTE);
		}
		else
		{
			mYear = SquirrelData.getYear();
			mMonth = SquirrelData.getMonth();
			mDay = SquirrelData.getDay();
			mHour = SquirrelData.getHour();
			mMinute = SquirrelData.getMinute();
		}

		// capture our View elements
		mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
		mPickTime = (Button) findViewById(R.id.pickTime);

		// add a click listener to the button
		mPickTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID);
			}
		});

		// display the current date
		updateDateDisplay();
		updateTimeDisplay();
	
		final  Button doneButton = (Button) findViewById(R.id.datetime_done);
		doneButton.setOnClickListener(this);
		
	}

	// updates the date in the TextView
	private void updateDateDisplay() {
		mDateDisplay.setText(
				new StringBuilder()
				// Month is 0 based so add 1
				.append(mMonth + 1).append("-")
				.append(mDay).append("-")
				.append(mYear).append(" "));
	}
	
	private void updateTimeDisplay() {
		mTimeDisplay.setText(
				new StringBuilder()
				.append(pad(mHour)).append(":")
				.append(pad(mMinute)));
	}

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	// the callback received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener mDateSetListener =
		new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, 
				int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDateDisplay();
		}
	};
	// the callback received when the user "sets" the time in the dialog
	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
		new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute;
			updateTimeDisplay();
		}
	};
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this,
					mDateSetListener,
					mYear, mMonth, mDay);
		case TIME_DIALOG_ID:
	        return new TimePickerDialog(this,
	                mTimeSetListener, mHour, mMinute, false);
		}
		return null;
	}

	public void onClick(View v) {
		//check to make sure date/time are not in the future, if they are, warn user
		Calendar c = Calendar.getInstance();
		Calendar m = Calendar.getInstance();
		m.set(mYear, mMonth, mDay, mHour, mMinute);
		if(m.after(c))
		{
			AlertDialog.Builder builder;
			AlertDialog alertDialog; 
			Context context = v.getContext();
			
			builder = new AlertDialog.Builder(context);		
			builder.setTitle("Future Date & Time");
			builder.setMessage("The date and time you entered is in the future.  Do you wish to proceed?");
			
			builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
		        public void onClick(DialogInterface dialog, int whichButton) {
					SquirrelData.setDate(mYear, mMonth, mDay);
					SquirrelData.setTime(mHour, mMinute);
					SquirrelData.setDateTimeCheckMark(true);
		        	finish();
		        } 
		        });	
			builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
		        public void onClick(DialogInterface dialog, int whichButton) {
		        	//finish();
		        } 
		        });					
			alertDialog = builder.create();
			alertDialog.show();
		}
		else
		{
			SquirrelData.setDate(mYear, mMonth, mDay);
			SquirrelData.setTime(mHour, mMinute);
			SquirrelData.setDateTimeCheckMark(true);
			finish();
		}
	}
}